const Router = require('koa-router')
const router = new Router({
    prefix: '/v1/good'
});
// const { IntValidator }  = require('../../validators/validator')

const { GoodClass } = require('../../models/good/good_class')
const { GoodSpec } = require('../../models/good/good_spec')
const { Goods } = require('../../models/good/goods')

/**
 * 获取主要商品分类
 */
router.get('/good_class_index', async ctx => {
    const where = { parent_id: 0 }
    const res = await GoodClass.findAll({where})
    ctx.body = {
        data: res
    }
})

/**
 * 根据分类ID 查询子分类
 */
router.post('/good_child_class', async ctx => {
    const body = ctx.request.body;
    const { id } = body;
    const where = { parent_id: id }
    const res = await GoodClass.findAll({where})
    const parent = { id:id };
    const p = await GoodClass.findOne({parent})
    ctx.body = {
        data: {
            list : res,
            theme : p.cover_img
        }
    }
})



/**
 * 根据分类ID 查询商品
 */
router.post('/find_by_class_id', async (ctx, next) => {
    const body = ctx.request.body;
    const { class_id } = body;
    const res = await Goods.findAndCountAll({
        where: { class_id },
        limit: 10,
        offset: 0
    })
    ctx.body = {
        data: res
    };
})

/**
 * 根据商品ID查询 商品
 */
router.post('/find_by_good_id', async (ctx, next) => {

    const body = ctx.request.body;
    const { id } = body;
    const res = await Goods.findOne({
        where: { id },
    })
    ctx.body = {
        data: res
    };
})




/**
 * 根据商品规格查询商品的具体价格
 */
router.post('/find_sku_by_name', async (ctx, next) => {
    
    const body = ctx.request.body;
    const { product_specs } = body;
    const res = await GoodSpec.findOne({
        where: { product_specs },
    })
    ctx.body = {
        data: res
    };
})

module.exports = router;